//Nancy Wen
//September 28, 2010
//This is our number game where the user has to type in the sum for 
//the numbers floating on the spring.

//The things to do:

/*
 * have multiple rounds
 * move numbers
 * add timer
 * add point counter
 * 
 */

import java.applet.Applet;
import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Stroke;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class NumberGame extends Game
{
	ArrayList<Integer> currentNums = new ArrayList<Integer>();
	int currentSum;
	double currentLevel; 
	int iq = 100;
	boolean printNums = true;
	boolean paint = true;
	String message;

	JPanel top = new JPanel(new GridLayout(3,2));
	GraphPaper middle = new GraphPaper();
	JPanel bottom = new JPanel(new GridLayout(1,4));

	JLabel timeLabel = new JLabel ("Time left: ");
	JLabel levelLabel = new JLabel ("Current Level: ");
	JLabel iqLabel = new JLabel ("IQ Score: ");

	JTextField timeField = new JTextField();
	JTextField levelField = new JTextField();
	JTextField iqField = new JTextField(iq + "");

	JLabel blankLabel = new JLabel("");
	JLabel answerLabel = new JLabel("The sum is: ");
	JTextField answerField = new JTextField();
	JButton submit = new JButton ("Submit");
	JButton playAgain = new JButton("Run Again");

	public NumberGame()
	{
		setSize(600,600);
		setLayout(new BorderLayout());

		top.add(timeLabel);
		top.add(timeField);
		top.add(levelLabel);
		top.add(levelField);
		top.add(iqLabel);
		top.add(iqField);

		bottom.add(answerLabel);
		bottom.add(answerField);
		bottom.add(submit);
		bottom.add(playAgain);

		add(top, BorderLayout.NORTH); 
		add(middle, BorderLayout.CENTER); 
		add(bottom, BorderLayout.SOUTH);

		submit.addActionListener(new ButtonAction());
		playAgain.addActionListener(new ButtonAction());

		currentLevel = 1.0;    


		currentNums = generateNum(currentLevel);
		for(int i = 0; i< currentNums.size(); i++)
		{
			currentSum = currentSum + currentNums.get(i);
		}//closes for loop
		printNums = true;
		
		
	}//closes NumberGame constructor()

	//this method generates random numbers to sum
	public ArrayList<Integer> generateNum(double level)
	{
		ArrayList<Integer> listOfNums = new ArrayList<Integer>();

		int superLevel = (int)level; //range of nums
		int subLevel = (int)((level * 5) % 5 + 1); 

		for(int i = 0; i < subLevel + 1; i++)
		{
			int tempNum = (int) (Math.ceil( Math.random() * superLevel * 10));
			listOfNums.add(new Integer(tempNum));
		}//closes for loop	

		return listOfNums;
	}//closes method 

	private class GraphPaper extends JPanel
	{
		public void paintComponent(Graphics g)
		{
			Graphics2D g2 = (Graphics2D)g;
			g.translate(500, 350); 
			g2.setColor(Color.BLACK);
			Stroke stroke1 = new BasicStroke (3f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND); 
			g2.setStroke (stroke1); 
			g2.fillRect(-400, -250, 800, 500);

			if(printNums == true)
			{
				int numOfNums = currentNums.size();
				for(int i = 0; i < numOfNums; i++) 
				{
					Color tempColor = new Color((int)(Math.random() * 100 + 155),
							(int)(Math.random() * 255 + 1),(int)(Math.random() * 100 + 155) );
					g2.setColor(tempColor);
					g2.setFont(new Font("Sans Serif", Font.BOLD, 32));
					int x = (int)(Math.random() * 700 - 350);
					int y = (int)(Math.random() * 440 - 220);
					g2.drawString("" + currentNums.get(i), x, y); 
				}//closes for loop
			}//closes if

			if(printNums == false)
			{
				Color tempColor = new Color(255, 255, 255);
				g2.setColor(tempColor);
				g2.setFont(new Font("Sans Serif", Font.BOLD, 32));
				g2.drawString(message, -50, -50);
				//printNums == true
			}//closes if statement
		}//closes method paintComponent
	}//closes class GraphPaper


	private class ButtonAction implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			if(e.getSource() == submit)
			{
				int userInput = Integer.parseInt((answerField.getText()));
				if(userInput == currentSum)
				{
					paint = false; 	
					message = "Congrats";
					printNums = false;
					currentLevel = currentLevel + 0.1;
					repaint();

				}//closes if
				else
				{
					paint = false; 	
					printNums = false;
					message = "Wrong. YOU LOSE";
					if(currentLevel >= 1.2)
					{
						currentLevel = currentLevel - 0.1;
					}//closes if statement
					repaint(); 
				}//closes else
			}//closes if submit

			if(e.getSource() == playAgain)
			{					
				answerField.setText("");
				printNums = true;

				currentNums = generateNum(currentLevel);
				currentSum = 0;
				//the sum changes
				for(int i = 0; i< currentNums.size(); i++)
				{
					currentSum = currentSum + currentNums.get(i);
				}//closes for loop
				repaint();
			}//closes if runAgain
		}//closes method actionPerformed
	}//closes class ButtonAction
}//closes class NumberGame